var PromoPrice = new Class({
    Implements: Options,
    options: {
        tag: null,
        page: 'promo'
    },
    data: null,
    itemID: 0,
    email: '',
    initialize: function(opts) {
        this.setOptions(opts);

        if(!document.id(this.options.tag)) return;

        var href = new URI(document.location.href);
        this.itemID = href.getData('iid');
        this.email = href.getData('email');

        if(!this.itemID || !this.email) {
            alert('not defined itemID or Email.');
            return;
        }

        // get item and user information + order information
        this.requestGoogle = new Request({
            url: '/Admin/webstat/getFromMySQL',
            method: 'get',
            onRequest: function() {
                new ShowBox().say('Connect to db...');
            },
            onComplete: function(resp) {
                try {
                    this.data = eval('(' + resp + ')');
                }
                catch(err) { alert('Error... promoPrice:' + err); return; }

                this.showData();
            }.bind(this)
        }).send('table=dppc&ex=getPromoPrice&iid=' + this.itemID + '&email=' + this.email + '&nc=' + Date.now());

    },
    showData: function() {

        new Element('div[style=font-size:16px;color:green;margin:3px 0 2px;][text=Email: ' + this.email + ']').inject(this.options.tag);

        new Element('table[cellspacing=1][cellpadding=1][style=width:auto;border:solid 1px silver;background-color:white;]').adopt(
            new Element('tbody').adopt(
                new Element('tr').adopt(
                    new Element('td#cvrow').adopt(
                        new Element('span#itName[html=' + this.data.item.name + ']'),
                        new Element('div').adopt(
                            new Element('ul').adopt(
                                new Element('li[html=&#187; SKU : ' + this.data.item.sku + ']'),
                                new Element('li[html=&#187; Vendor : ' + this.data.item.vendor + ']'),
                                new Element('li[html=&#187; mfrName : ' + this.data.item.mfrName + ']'),
                                new Element('li[html=&#187; mfrNumber : ' + this.data.item.mfrNumber + ']'),
                                new Element('li[html=&#187; Old date : ' + this.data.item.stockDate + ']')
                            )
                        ),
                        new Element('div').adopt(
                            new Element('ul').adopt(
                                new Element('li[html=&#187; Cost price : $' + this.data.item.costPrice.toFixed(2) + ']'),
                                new Element('li[html=&#187; Sell price : $' + this.data.item.sellPrice.toFixed(2) + ']'),
                                new Element('li[html=&#187; Profit : ' + ((this.data.item.sellPrice - this.data.item.costPrice)  / this.data.item.sellPrice * 100).toFixed(2) + '%]'),
                                new Element('li[html=&#187; Reseller price : $' + this.data.item.resPrice.toFixed(2) + ']'),
                                new Element('li[html=&#187; Drop Price : $' + this.data.item.dropPrice.toFixed(2) + ']')
                            )
                        ),
                        this.makeRadio()
                    )
                )
            )
        ).inject(this.options.tag);


        // customer
        if(this.data.cust && this.options.page == 'promo') {
            new Element('div[style=font-size:16px;color:green;margin:3px 0 2px;][text=Customer :]').inject(this.options.tag);

            new Element('div').adopt(
                new Element('table[cellspacing=1][cellpadding=1][style=width:400px;border:solid 1px silver;background-color:white;]').adopt(
                    new Element('tbody').adopt(
                        new Element('tr[style=background:lightblue;font-size:14px;]').adopt(
                            new Element('th[html=&nbsp;]'),
                            new Element('th[text=Shipping]'),
                            new Element('th[text=Billing]')
                        ),
                        new Element('tr[style=background:#ffffcf;font-size:12px;]').adopt(
                            new Element('td[text=Company :]'),
                            new Element('td[text=' + this.data.cust.shipp.company + ']'),
                            new Element('td[text=' + this.data.cust.bill.company + ']')
                        ),
                        new Element('tr[style=background:#ffffff;font-size:12px;]').adopt(
                            new Element('td[text=Name :]'),
                            new Element('td[text=' + this.data.cust.shipp.name + ']'),
                            new Element('td[text=' + this.data.cust.bill.name + ']')
                        ),
                        new Element('tr[style=background:#ffffcf;font-size:12px;]').adopt(
                            new Element('td[text=Address :]'),
                            new Element('td[text=' + this.data.cust.shipp.address + ']'),
                            new Element('td[text=' + this.data.cust.bill.address + ']')
                        ),
                        new Element('tr[style=background:#ffffff;font-size:12px;]').adopt(
                            new Element('td[text=City :]'),
                            new Element('td[text=' + this.data.cust.shipp.city + ']'),
                            new Element('td[text=' + this.data.cust.bill.city + ']')
                        ),
                        new Element('tr[style=background:#ffffcf;font-size:12px;]').adopt(
                            new Element('td[text=State :]'),
                            new Element('td[text=' + this.data.cust.shipp.state + ']'),
                            new Element('td[text=' + this.data.cust.bill.state + ']')
                        ),
                        new Element('tr[style=background:#ffffff;font-size:12px;]').adopt(
                            new Element('td[text=Country :]'),
                            new Element('td[text=' + this.data.cust.shipp.country + ']'),
                            new Element('td[text=' + this.data.cust.bill.country + ']')
                        ),
                        new Element('tr[style=background:#ffffcf;font-size:12px;]').adopt(
                            new Element('td[text=Zip :]'),
                            new Element('td[text=' + this.data.cust.shipp.zip + ']'),
                            new Element('td[text=' + this.data.cust.bill.zip + ']')
                        ),
                        new Element('tr[style=background:#ffffff;font-size:12px;]').adopt(
                            new Element('td[text=Phone :]'),
                            new Element('td[text=' + this.data.cust.shipp.phone + ']'),
                            new Element('td[text=' + this.data.cust.bill.phone + ']')
                        ),
                        new Element('tr[style=background:#ffffcf;font-size:12px;]').adopt(
                            new Element('td[text=Cell :]'),
                            new Element('td[text=' + this.data.cust.shipp.cell + ']'),
                            new Element('td[text=' + this.data.cust.bill.cell + ']')
                        ),
                        new Element('tr[style=background:#ffffff;font-size:12px;]').adopt(
                            new Element('td[text=Fax :]'),
                            new Element('td[text=' + this.data.cust.shipp.fax + ']'),
                            new Element('td[text=' + this.data.cust.bill.fax + ']')
                        )
                    )
                )
            ).inject(this.options.tag);
        }

        // orders
        var div = new Element('div[style=font-size:16px;color:red;margin:3px 0 2px;][text=No orders]');
        if(this.data.orders) {
            div.empty().set('text', 'Orders :');

            var tbody = new Element('tbody');
            new Element('table[cellspacing=1][cellpadding=1][style=width:1200px;border:solid 1px silver;background-color:white;color:black;]').adopt(
                tbody.adopt(
                    new Element('tr[style=background:lightblue;font-size:14px;]').adopt(
                        new Element('th[text=Order #]'),
                        new Element('th[text=Order Date]'),
                        new Element('th[text=COD]'),
                        new Element('th[text=Waranty]'),
                        new Element('th[text=Shipping]'),
                        new Element('th[text=Shipping Type]'),
                        new Element('th[text=Tax]'),
                        new Element('th[text=Total]')
                    )
                )
            ).inject(div);

            this.data.orders.each(function(order) {
                new Element('tr[style=background:#ffffcf;text-align:center;font-size:12px;]').adopt(
                    new Element('td[text=' + order.no + ']'),
                    new Element('td[text=' + order.date + ']'),
                    new Element('td[text=' + order.cod + ']'),
                    new Element('td[text=' + order.wrty + ']'),
                    new Element('td[text=$' + order.shipPrice.toFixed(2) + ']'),
                    new Element('td[text=' + order.shipCode + ']'),
                    new Element('td[text=$' + order.tax.toFixed(2) + ']'),
                    new Element('td[text=$' + order.amount.toFixed(2) + ']')
                ).inject(tbody)

                var td = new Element('td[colspan=7]');
                new Element('tr').adopt(
                    new Element('td[html=&nbsp;]'),
                    td
                ).inject(tbody)

                // orders items
                var itemtbody = new Element('tbody');
                td.adopt(
                    new Element('table[cellspacing=1][cellpadding=1][style=width:100%;]').adopt(
                        itemtbody.adopt(
                            new Element('tr[style=background:lightblue;font-size:14px;]').adopt(
                                new Element('th[text=Accware SKU]'),
                                new Element('th[text=Item Name]'),
                                new Element('th[text=Cost Price]'),
                                new Element('th[text=Sell Price]')
                            )
                        )
                    )
                );

                order.itms.each(function(item) {
                    itemtbody.adopt(
                        new Element('tr[style=text-align:right;font-size:12px;]').adopt(
                            new Element('td[text=' + item.sku + ']'),
                            new Element('td[text=' + item.name + ']'),
                            new Element('td[text=$' + item.costPrice.toFixed(2) + ']'),
                            new Element('td[text=$' + item.sellPrice.toFixed(2) + ']')
                        )
                    )
                });
                td.adopt(new Element('hr'));
            });
        }
        div.inject(this.options.tag);


    },
    makeRadio: function() {

        this.coupon = 'R' + ('' + Number.random(0, 999999) + 1000000).substring(1,7);
        var div = new Element('div#coupon[style=margin-top:10px;color:navy;]').adopt(
            new Element('div[style=color:green;][text=CV' + this.coupon + ']')
        );

        //
        var myArray = [];
        for(var i = 1; i < 100; i++) {
            myArray.append([i]);
        }
        this.percent = new Element('select[style=width:160px;]');
        new Element('option[value=0][title=0][text=-= Select Percent =-]').inject(this.percent);
        var price;
        myArray.each(function(percent) {
            price = this.data.item.sellPrice - (this.data.item.sellPrice * (percent / 100));
            new Element('option[value=' + price.toFixed(2) + '][title=' + percent + '][html=&nbsp;&nbsp;(' + percent + '%) <b>$' + numberFormat(price) + '</b> <em style="color:red;">$' + numberFormat(this.data.item.sellPrice - price) + ' off</em>]').inject(this.percent);
        }.bind(this));
        this.percent.inject(div);

        //
        var date = new Element('input#date[type=text][name=endDate][size=10][maxlength=10]').inject(div);
	new DatePicker(date, {
            startDate: new Date().increment('day', 30)
        });

        //
        new Element('button[text=Create New Coupon]',{
            'events': {
                'click': this.saveCoupon.bind(this),
                'focus': function() {this.blur();}
            }
        }).inject(div);

        return div;
    },
    saveCoupon: function(ev) {
        ev.stop();

        if(this.percent.getElement(':selected').get('title') != '0') {
            ev.target.set('disabled', true);

            new Request({
                url: '/Admin/webstat/getFromMySQL',
                method: 'post',
                onRequest: function() {
                    new ShowBox().say('Save Coupon...');
                }
            }).send('table=dppc&ex=savePromoPrice&itemid=' + this.data.item.id +
                    '&sellPrice=' + this.data.item.sellPrice.toFixed(2) +
                    '&promCode=' + this.coupon +
                    '&promPrice=' + this.percent.getElement(':selected').get('value') +
                    '&percent=' + this.percent.getElement(':selected').get('title') +
                    '&expDate=' + document.id('date').get('value') +
                    '&email=' + this.email +
                    '&itmName=' + this.data.item.name +
                    '&page=' + this.options.page +
                    '&nc=' + Date.now());

        }
        else {
            alert('Not selected promotion price');
        }
    }
});